﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace mjRegister
{
    public class ConfigData
    {
        public string ProxyServer { get; set; }     //代理服务器地址
        public int ProxyPort { get; set; }          //代理服务器端口
        public string ProxyUser { get; set; }       //代理服务器用户
        public string ProxyPass { get; set; }       //代理服务器用户密码
    }

    public static class ConfigHelper
    {
        public static ConfigData ReadGlobalConfigData(string filename)
        {
            ConfigData config = new ConfigData();
            try
            {
                string[] lines = File.ReadAllLines(filename);
                foreach (string line in lines)
                {
                    if (line.StartsWith("ProxyServer=", StringComparison.OrdinalIgnoreCase))
                    {
                        config.ProxyServer = GetStringValue(line);
                        continue;
                    }

                    if (line.StartsWith("ProxyPort=", StringComparison.OrdinalIgnoreCase))
                    {
                        config.ProxyPort = GetIntegerValue(line);
                        continue;
                    }

                    if (line.StartsWith("ProxyUser=", StringComparison.OrdinalIgnoreCase))
                    {
                        config.ProxyUser = GetStringValue(line);
                        continue;
                    }

                    if (line.StartsWith("ProxyPass=", StringComparison.OrdinalIgnoreCase))
                    {
                        config.ProxyPass = GetStringValue(line);
                        continue;
                    }
                }
            }
            catch (Exception e)
            {
                return null;
            }

            return config;
        }

        public static bool GetBoolValue(string line)
        {
            return ("1" == GetStringValue(line));
        }

        public static int GetIntegerValue(string line)
        {
            string value = GetStringValue(line);

            return Common.GetInteger(value);
        }

        public static string GetStringValue(string line)
        {
            return line.Substring(line.IndexOf('=') + 1);
        }
    }
}
